MsgN("-- spawnmenu.lua loaded")

hook.Add("SpawnMenuEnabled", "MDRP.SpawnMenuEnabled", function()
	if (!MDRP.IsEnabled()) then return end
	return false
end)

hook.Add("SpawnMenuOpen", "MDRP.SpawnMenuOpen", function()
	if (!MDRP.IsEnabled()) then return end
	return false
end)

hook.Add("OnSpawnMenuOpen", "MDRP.OnSpawnMenuOpen", function()
	if (!MDRP.IsEnabled()) then return end
	if (!MENU) then MENU = vgui.Create("menu") end

	MENU:Open()
end)

hook.Add("OnSpawnMenuClose", "MDRP.OnSpawnMenuClose", function()
	MENU:Close()
	MENU:Remove()
	MENU = nil
end)




local PANEL = {}

function PANEL:Init()
	if (!tools) then tools = spawnmenu.GetTools() end

	self:SetTitle( "" )
	self:SetDraggable( false )
	self:SetDeleteOnClose( true )
	self:ShowCloseButton(false)

	self:SetSize(ScrW() - 50, ScrH() - 50)
	self:SetPos(25, 25)

	self.Paint = function()
		surface.SetDrawColor(0, 0, 0, 0)
		surface.DrawRect(0, 0, MENU:GetWide(), MENU:GetTall())
	end




	self.PropsPanel = vgui.Create("DPropertySheet")
	self.PropsPanel:SetParent( self )
	self.PropsPanel:SetSize( (self:GetWide()*.5 - 5), self:GetTall() )
	self.PropsPanel:SetPos( 0, 0 )



	self.PropsPanel:AddSheet("Props", vgui.Create("mdrp_props"), "gui/silkicons/brick_add", false, false)

	--self.Props:AddCategory( "Wood - Tables/Desks", MDRP.SpawnList["Wood - Tables/Desks"] )



	self.ToolPanel = vgui.Create("DPropertySheet")
	self.ToolPanel:SetParent(self)
	self.ToolPanel:SetSize((self:GetWide()*.5 - 5), self:GetTall())
	self.ToolPanel:SetPos((self:GetWide()*.5 + 5), 0)


	for _, tool in pairs( tools ) do
		--Items
		--Icon
		--Name
		--Label
		self[tool.Name] = vgui.Create("MDMenu")

		self[tool.Name]:List( self.ToolPanel )

		self.ToolPanel:AddSheet(tool.Label, self[tool.Name].Background, tool.Icon, false, false)

		for _, items in pairs( tool.Items ) do
			if (type(items) == "table") and (!table.HasValue( MDRP.ProhibitedTools, string.lower(items.ItemName) )) then

				local name = items.ItemName
				local label = items.Text
				local _items = table.Copy( items )

				_items.ItemName = nil
				_items.Text = nil

				self[tool.Name]:AddCategory( name, label, _items )
			end
		end
	end
end

function PANEL:Open()
	self:SetVisible( true )

	gui.EnableScreenClicker(true)
	RestoreCursorPosition()
end

function PANEL:Close()
	self.BaseClass.Close( self )

	RememberCursorPosition()
	gui.EnableScreenClicker(false)
end

vgui.Register("menu", PANEL, "DFrame")




--start vgui MDMenu
local PANEL = {}

/*---------------------------------------------------------
   Name: Init
---------------------------------------------------------*/
function PANEL:Init()

	self.Background = vgui.Create("DPanelList")
	self.Background:EnableHorizontal(true)
	self.Background:EnableVerticalScrollbar(false)
	self.Background:SetAutoSize(false)
	self.Background:SetPadding(0)
	self.Background:SetSpacing(5)
	self.Background:SetSize(0, 0)
	self.Background:SetPos(0, 0)
	self.Background.Paint = function()
		surface.SetDrawColor(170, 170, 170, 255)
		surface.DrawRect(0, 0, self.Background:GetWide(), self.Background:GetTall())
	end

end

function PANEL:List( panel )

	self.ToolList = vgui.Create("DPanelList")
	self.Background:AddItem(self.ToolList)
	self.ToolList:EnableVerticalScrollbar(true)
	self.ToolList:SetSize( panel:GetWide()*.33, panel:GetTall() - 31)
	self.ToolList:SetPos(0, 0)
	self.ToolList:SetAutoSize( false )
	self.ToolList:SetSpacing( 1 )
	self.ToolList:SetPadding( 0 )

	self.Content = vgui.Create("DPanelList")
	self.Background:AddItem(self.Content)
	self.Content:SetSize( panel:GetWide()*.64, panel:GetTall() - 31)
	self.Content:EnableVerticalScrollbar(false)
	self.Content:SetSpacing(0)
	self.Content:SetPadding(5)

end

function PANEL:AddCategory( Name, Label, tItems )

	self.Category = vgui.Create( "DCollapsibleCategory")
 	self.ToolList:AddItem( self.Category )
 	self.Category:SetLabel( Label )
 	self.Category:SetCookieName( "ToolMenu."..tostring(Name) )

 	self.CategoryContent = vgui.Create( "DPanelList" )
 	self.CategoryContent:SetAutoSize( true )
 	self.CategoryContent:SetDrawBackground( false )
 	self.CategoryContent:SetSpacing( 0 )
 	self.CategoryContent:SetPadding( 0 )
 	self.Category:SetContents( self.CategoryContent )

	local bAlt = true

 	for _, toolInfo in pairs( tItems ) do
		if (!table.HasValue( MDRP.ProhibitedTools, toolInfo.ItemName )) then
			local tool = vgui.Create( "ToolMenuButton", self )

			tool:SetText( toolInfo.Text )
			tool.OnSelect = function( button )
				LastSelected = toolInfo.Command
				self:EnableControlPanel( button )
			end

			concommand.Add( Format( "tool_%s", toolInfo.ItemName ), function() tool:OnSelect() end )

			if ( toolInfo.SwitchConVar ) then tool:AddCheckBox( toolInfo.SwitchConVar ) end

			tool.ControlPanelBuildFunction = toolInfo.CPanelFunction
			tool.Command = toolInfo.Command
			tool.Name = toolInfo.ItemName
			tool.Controls = toolInfo.Controls
			tool.Text = toolInfo.Text

			tool:SetAlt( bAlt )
			bAlt = !bAlt

			self.CategoryContent:AddItem( tool )
		end
 	end
end

function PANEL:EnableControlPanel( button )

	if ( self.LastSelected ) then self.LastSelected:SetSelected( false ) end

	button:SetSelected( true )
	self.LastSelected = button

	local cp = controlpanel.Get( button.Name )
	if ( !cp:GetInitialized() ) then
		cp:FillViaTable( button )
	end

	self.Content:Clear()
	self.Content:AddItem( cp )
	self.Content:Rebuild()

	g_ActiveControlPanel = cp

	if ( button.Command ) then LocalPlayer():ConCommand( button.Command ) end

 end

vgui.Register( "MDMenu", PANEL, "Panel" )




--start vgui MDPropMenu
local PANEL = {}

/*---------------------------------------------------------
   Name: Init
---------------------------------------------------------*/
function PANEL:Init()

	self.Background = vgui.Create("DPanelList")
	self.Background:EnableHorizontal(true)
	self.Background:EnableVerticalScrollbar(true)
	self.Background:SetAutoSize(false)
	self.Background:SetPadding(0)
	self.Background:SetSpacing(5)
	self.Background:SetSize(0, 0)
	self.Background:SetPos(0, 0)
	self.Background.Paint = function()
		surface.SetDrawColor(170, 170, 170, 255)
		surface.DrawRect(0, 0, self.Background:GetWide(), self.Background:GetTall())
	end

end

function PANEL:List( panel )

	self.ToolList = vgui.Create("DPanelList")
	self.Background:AddItem(self.ToolList)
	self.ToolList:EnableVerticalScrollbar(true)
	self.ToolList:SetSize( panel:GetWide() * .33, panel:GetTall() - 31)
	self.ToolList:SetPos(0, 0)
	self.ToolList:SetAutoSize( false )
	self.ToolList:SetSpacing( 1 )
	self.ToolList:SetPadding( 0 )

	self.Content = vgui.Create("DPanelList")
	self.Background:AddItem(self.Content)
	self.Content:SetSize( panel:GetWide() * .64, panel:GetTall() - 31)
	self.Content:EnableVerticalScrollbar(false)
	self.Content:SetSpacing(0)
	self.Content:SetPadding(5)

end

vgui.Register( "MDPropMenu", PANEL, "Panel" )











--[[
	Props Panel
]]
local PANEL = {}

function PANEL:Init()

	self.Label = vgui.Create( "DLabel", self )
	self.Label:SetText( "Click on an icon to spawn a prop." )
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
	self.Label:SizeToContents( )

	self.List = vgui.Create( "DPanelList", self)
	self.List:SetPos( 25,25 )
	self.List:SetSize(ScrW() / 2 - 62, ScrH() - 104)

	self.List:SetSpacing( 5 )
	self.List:EnableHorizontal( false )
	self.List:EnableVerticalScrollbar( true )

	self.Menus = {}
	self.IconList = {}
	self.ValueTable = {}
	
	local iteration = 1
	for k,v in pairs( GMS_SpawnLists ) do
		self.ValueTable.iteration = k
		self.Menus[k] = {}
		self.Menus[k] = vgui.Create("DCollapsibleCategory", self.List)
		self.Menus[k]:SetPos( 25,50*iteration )
		self.Menus[k]:SetSize( ScrW() / 2 - 76, 50 )
		self.Menus[k]:SetExpanded( 0 )
		self.Menus[k]:SetLabel( k )
		iteration = iteration + 1
		
		self.IconList[k] = {}
		self.IconList[k] = vgui.Create( "DPanelList", self.Menus[k] )
		self.IconList[k]:EnableVerticalScrollbar( true )
		self.IconList[k]:EnableHorizontal( true )
		self.IconList[k]:SetAutoSize( true )
		self.IconList[k]:SetSpacing( 5 )
		self.IconList[k]:SetPadding( 4 )
		self.IconList[k]:SetVisible( true )
		
		self.Menus[k]:SetContents( self.IconList[k] )
		CategoryList:AddItem(self.Menus[k])

		for _, model in pairs (v) do
			local Icon = vgui.Create( "SpawnIcon", self.IconList[k] )
			Icon:SetModel(model)
			Icon.DoClick = function( Icon )
				RunConsoleCommand("gm_spawn", model, 0)
			end
			Icon:SetIconSize( 64 )
			Icon:InvalidateLayout( true )
			Icon:SetToolTip( Format( "%s", model ) )
			self.IconList[k]:AddItem( Icon )
		end
	end
end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.Label:SetPos( 2, 2 )
	for k,v in pairs (self.ValueTable) do
		self.IconList[v]:StretchToParent( 4, 26, 4, 4 )
		self.IconList[v]:InvalidateLayout()
	end
end

vgui.Register( "mdrp_props", PANEL, "Panel" )

--[[
	Settings Panel
]]
local PANEL = {}

function PANEL:Init()

end

vgui.Register( "mdrp_settings", PANEL, "Panel" )

--[[
	Help Panel
]]
local PANEL = {}

function PANEL:Init()

end

vgui.Register( "mdrp_help", PANEL, "Panel" )

--[[
	Actions Panel
]]
local PANEL = {}

function PANEL:Init()
	self.Background = vgui.Create("DPanelList")
	self.Background:EnableVerticalScrollbar(false)
	self.Background:SetSize((self.ToolPanel:GetWide() - 10), self.ToolPanel:GetTall())
	self.Background:SetPos(0, 0)
	self.Background.Paint = function()
		local Wide = self.Background:GetWide()
		local Height = self.Background:GetTall()

		local Title = "The 6 golden rules you must follow at all times:"
		local One = "Do not hold up a fight round, even if no one is dead."
		local Two = "Attatch/constraint/stick props to the map."
		local Three = "Spam mic/chat/props in any way."
		local Four = "Don't CONSTANTLY build sharks, you can, just dont push it."
		local Five = "Don't be disrespectful in any wat to other players."
		local Six = "Don't exploit any bugs/glitches/errors with the gamemode."
		local Seven = "If any of these rules are broken, you will be kicked/banned on your fault."
		surface.SetDrawColor(50, 50, 50, 255)
		surface.DrawRect(0, 0, Wide, Height)
		surface.SetTextColor(255, 255, 255, 255)

		surface.SetFont("Trebuchet20")
		local TitleW, TitleH = surface.GetTextSize(Title)
		surface.SetTextPos(((Wide*.5) - (TitleW*.5)), ((Height*.03) - (TitleH*.5)))
		surface.DrawText(Title)

		surface.SetFont("Default")
		local OneW, OneH = surface.GetTextSize(One)
		surface.SetTextPos(((Wide*.5) - (OneW*.5)), ((Height*.09) - (OneH*.5)))
		surface.DrawText(One)

		surface.SetFont("Default")
		local TwoW, TwoH = surface.GetTextSize(Two)
		surface.SetTextPos(((Wide*.5) - (TwoW*.5)), ((Height*.12) - (TwoH*.5)))
		surface.DrawText(Two)

		surface.SetFont("Default")
		local ThreeW, ThreeH = surface.GetTextSize(Three)
		surface.SetTextPos(((Wide*.5) - (ThreeW*.5)), ((Height*.15) - (ThreeH*.5)))
		surface.DrawText(Three)

		surface.SetFont("Default")
		local FourW, FourH = surface.GetTextSize(Four)
		surface.SetTextPos(((Wide*.5) - (FourW*.5)), ((Height*.18) - (FourH*.5)))
		surface.DrawText(Four)

		surface.SetFont("Default")
		local FiveW, FiveH = surface.GetTextSize(Five)
		surface.SetTextPos(((Wide*.5) - (FiveW*.5)), ((Height*.21) - (FiveH*.5)))
		surface.DrawText(Five)

		surface.SetFont("Default")
		local SixW, SixH = surface.GetTextSize(Six)
		surface.SetTextPos(((Wide*.5) - (SixW*.5)), ((Height*.24) - (SixH*.5)))
		surface.DrawText(Six)

		surface.SetTextColor(200, 25, 25, 255)
		surface.SetFont("Default")
		local SevenW, SevenH = surface.GetTextSize(Seven)
		surface.SetTextPos(((Wide*.5) - (SevenW*.5)), ((Height*.27) - (SevenH*.5)))
		surface.DrawText(Seven)
		surface.SetTextColor(255, 255, 255, 255)
	end
end

vgui.Register( "mdrp_actions", PANEL, "Panel" )

--[[
	Admin Panel
]]
local PANEL = {}

function PANEL:Init()

end

vgui.Register( "mdrp_actions", PANEL, "Panel" )
